﻿using OrderManage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrderManageForm
{
    public partial class Form2 : Form
    {
        bool isAdd=false, isModifyDetail=false;
        bool isNew = false;
        public Form2(string s)
        {
            InitializeComponent();
            this.label1.Text = s;
            if (s == "新建") { isNew = true; }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            OrderDb o = Program.service.findID(id.Text.ToString());
            if (o == null) { return; }
            /*for (int i = 0; i < o.orderDetails.Count(); i++)
            {
                int index = this.dataGridView2.Rows.Add();
                this.dataGridView2.Rows[index].Cells[0].Value = i + 1 + "";
                this.dataGridView2.Rows[index].Cells[1].Value = o.orderDetails.goods[i].name;
                this.dataGridView2.Rows[index].Cells[2].Value = o.orderDetails.numbers[i].ToString();
                this.dataGridView2.Rows[index].Cells[3].Value = o.orderDetails.goods[i].price.ToString();
            }*/
            this.dataGridView2.DataSource = o.orderDetails;
        }

        private void button2_Click(object sender, EventArgs e)//增加明细
        {
            isAdd = true;
            string id = this.id.Text;
            string customer = this.customer.Text;
            string date = this.date.Text;
            if (id != "" && customer != "" && date != "")
            {
                int index = this.dataGridView2.Rows.Add();
                this.dataGridView2.Rows[index].Cells[0].Value = index+1;
                this.dataGridView2.ReadOnly = false;
                this.dataGridView2.Columns["序号"].ReadOnly = true;//序号不能改
            }
        }

        private void button1_Click(object sender, EventArgs e)//修改明细
        {
            isModifyDetail = true;
            this.dataGridView2.ReadOnly = false;
            this.dataGridView2.Columns["序号"].ReadOnly = true;//序号不能改
            this.dataGridView2.Columns["货物"].ReadOnly = true;//货物名不能改
        }

        private void button3_Click(object sender, EventArgs e)//删除明细
        {
            string id = this.id.Text;
            string customer = this.customer.Text;
            string date = this.date.Text;
            if (id != "" && customer != "" && date != "")
            {
                OrderDb o = Program.service.findID(id);
                if (this.dataGridView2.SelectedRows.Count == 0) { return; }
                int index = this.dataGridView2.SelectedRows[0].Index;
                this.dataGridView2.Rows.Remove(this.dataGridView2.SelectedRows[0]);
                var query = o.orderDetails.FirstOrDefault(d=>d.detailId==index);
                o.orderDetails.Remove(query);
                this.dataGridView2.Columns["序号"].ReadOnly = false;
                for (int i = index; i < dataGridView2.Rows.Count-1; i++)
                {
                    this.dataGridView2.Rows[index].Cells[0].Value = i+1+"";
                }
                this.dataGridView2.Columns["序号"].ReadOnly = true;
            }
        }

        private void button4_Click(object sender, EventArgs e)//保存订单
        {
            string id = this.id.Text;
            string customer = this.customer.Text;
            string date = this.date.Text;
            if (id != "" && customer != "" && date != "")
            {
                if (isNew)
                {
                    if (!Program.service.addOrder(new Order(id, customer, date)))
                    {
                        MessageBox.Show("已存在相同单号或同一客户同一天订单。","error");
                        return;
                    }
                    isNew = false;
                }
                OrderDb o = Program.service.findID(id);
                this.id.ReadOnly = true;
                Program.service.modifyCustomer(id,customer);
                Program.service.modifyDate(id,date);
                if (isAdd)//保存增加的明细
                {
                    if (this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[1].Value!=null && this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[2].Value != null && this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[3].Value != null)
                    {
                        OrderDetailDb d = new OrderDetailDb()
                        {
                            detailId = this.dataGridView2.Rows.Count,
                            goodsName = this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[1].Value.ToString(),
                            goodsPrice = Double.Parse(this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[3].Value.ToString()),
                            goodsNumber = int.Parse(this.dataGridView2.Rows[this.dataGridView2.Rows.Count - 2].Cells[2].Value.ToString())
                        };
                        o.orderDetails.Add(d);
                        this.dataGridView2.Rows.Clear();
                        this.dataGridView2.DataSource = o.orderDetails;
                        /*for (int i = 0; i < o.orderDetails.count; i++)
                        {
                            int index = this.dataGridView2.Rows.Add();
                            this.dataGridView2.Rows[index].Cells[0].Value = i + 1 + "";
                            this.dataGridView2.Rows[index].Cells[1].Value = o.orderDetails.goods[i].name;
                            this.dataGridView2.Rows[index].Cells[2].Value = o.orderDetails.numbers[i].ToString();
                            this.dataGridView2.Rows[index].Cells[3].Value = o.orderDetails.goods[i].price.ToString();
                        }*/
                    }
                    else
                    {
                        MessageBox.Show("新建订单明细信息不全。","error");
                        return;
                    }
                    this.dataGridView2.ReadOnly = true;
                    isAdd = false;
                }
                if (isModifyDetail)//保存修改明细
                {
                    if (this.dataGridView2.SelectedRows.Count == 0) { return; }
                    if (this.dataGridView2.SelectedRows[0].Cells[1].Value.ToString()!=""&& this.dataGridView2.SelectedRows[0].Cells[2].Value.ToString() != ""&& this.dataGridView2.SelectedRows[0].Cells[3].Value.ToString() != "")
                    {
                        Program.service.modifyOrderDetail(o.OrderId, int.Parse(this.dataGridView2.SelectedRows[0].Cells[0].Value.ToString()),int.Parse(this.dataGridView2.SelectedRows[0].Cells[2].Value.ToString()),Double.Parse(this.dataGridView2.SelectedRows[0].Cells[3].Value.ToString()));
                        //o.orderDetails.modifyGoods(int.Parse(this.dataGridView2.SelectedRows[0].Cells[0].Value.ToString()),int.Parse(this.dataGridView2.SelectedRows[0].Cells[2].Value.ToString()),Double.Parse(this.dataGridView2.SelectedRows[0].Cells[3].Value.ToString()));
                    }
                    this.dataGridView2.ReadOnly = true;
                }
            }
            else
            {
                MessageBox.Show("订单基本信息不全。","error");
            }
        }
    }
}
